package cn.stimd.spring.beans.factory.support;

import cn.stimd.spring.beans.factory.config.BeanDefinition;
import org.springframework.core.ResolvableType;

import java.lang.reflect.Method;

/**
 * Created by Stimd
 * 项目地址：https://gitee.com/stimd/spring-wheel
 * 公众号：Java编程探微
 */
public class RootBeanDefinition extends AbstractBeanDefinition {
    volatile Class<?> resolvedTargetType;               //BeanDefinition最终的Class类型
    volatile ResolvableType factoryMethodReturnType;    //工厂方法的返回值类型
    Object resolvedConstructorOrFactoryMethod;          //可能是构造器或工厂方法
    boolean isFactoryMethodUnique = false;

    public RootBeanDefinition() {
        super();
    }

    public RootBeanDefinition(Class<?> beanClass){
        super();
        setBeanClass(beanClass);
    }

    public RootBeanDefinition(BeanDefinition original){
        super(original);
    }


    public RootBeanDefinition(RootBeanDefinition original) {
        super(original);
        this.isFactoryMethodUnique = original.isFactoryMethodUnique;
    }

    public Class<?> getTargetType() {
        if (this.resolvedTargetType != null) {
            return this.resolvedTargetType;
        }
        return null;
    }


    public void setUniqueFactoryMethodName(String name) {
        setFactoryMethodName(name);
        this.isFactoryMethodUnique = true;
    }


    public boolean isFactoryMethod(Method candidate) {
        return (candidate != null && candidate.getName().equals(getFactoryMethodName()));
    }


    public Method getResolvedFactoryMethod() {
        Object candidate = this.resolvedConstructorOrFactoryMethod;
        return (candidate instanceof Method ? (Method) candidate : null);
    }


    public RootBeanDefinition cloneBeanDefinition() {
        return new RootBeanDefinition(this);
    }
}
